package com.qs.apigateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import java.util.Arrays;

/**
 * @Desc:  跨域资源共享
 * @author: jianfeng
 * @Time: 2019/9/26 14:29
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter(){
        final UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();

        final CorsConfiguration config=new CorsConfiguration();

        //允许cookie跨域
        config.setAllowCredentials(true);
        //存放原始域
        config.setAllowedOrigins(Arrays.asList("*"));
        //请求头
        config.setAllowedHeaders(Arrays.asList("*"));
        //请求方法类型GET、POST、*
        config.addAllowedMethod("*");
        //缓存时间，同一时间，相同的跨域请求不进行检查
        config.setMaxAge(300L);

        source.registerCorsConfiguration("/**",config);
        return new CorsFilter(source);
    }

}
